package cn.m3a1.task;

import cn.m3a1.dao.UserDao;
import cn.m3a1.model.Sc;
import cn.m3a1.model.Studentofclass;
import cn.m3a1.model.Tclass;
import cn.m3a1.model.User;
import com.iceking.dynamicweb.FrmkView;
import com.iceking.dynamicweb.GlobalUtils;
import com.iceking.dynamicweb.Pagination;
import com.iceking.dynamicweb.TaskProcessor;
import net.sf.json.JSONObject;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wyj on 2019-06-02.
 *
 */
public class TclassStuTask extends TaskProcessor {
    private User loginUser;
    public Boolean panduan(){
        loginUser = (User) getSession().getAttribute("loginUser");
        if (loginUser==null){
            return false;
        }

        int type=loginUser.getType();

        if (type!=99){
            return false;
        }
        return  true;
    }
    /**
     * 班级管理首页
     * @throws Exception
     */
    public void home() throws  Exception{
        if (!panduan()){
            return;
        }
        int classid = getParamInt("id");
       //  String name=getParam("name");
         if (classid==0){
             classid=1;
         }
        FrmkView frmkView;
        UserDao userDao=new UserDao();


            //显示班级学生名单
            frmkView=new FrmkView("TclassStudent.html");
            //查询所有班级信息
            getDBConn().startTransaction();
            String sql = "select user.id ,user.`code`,user.`name` from user ,studentofclass" +
                    " where user.id=studentofclass.studentid and studentofclass.classid="+classid;
            ResultSet rs = getDBConn().query(sql);
            List<User> list = new ArrayList<>();
            while (rs.next()) {
                //Sc sc=new Sc();
                User user=new User();

               user.setId(rs.getInt("id"));
                user.setCode(rs.getString("code"));
                user.setName(rs.getString("name"));
                list.add(user);
            }
            rs.close();
            //查询所有班级的信息
            Pagination<Tclass> pgu = new Pagination<>();
            getDBConn().select(Tclass.class, pgu);
            List<Tclass> lius = pgu.getRows();

            //查询该班级的班级名
             pgu.appendWhere("where id=?",classid);
            getDBConn().select(Tclass.class, pgu);
            Tclass tclass = pgu.getFirstResult();
            getDBConn().stopTransaction();

            frmkView.addData("members",list);
            frmkView.addData("tclass",tclass);
           frmkView.addData("lius",lius);




        toFreemarker(frmkView);
    }
    public void findstuclass() throws Exception {
        if (!panduan()){
            return;
        }
        JSONObject json = new JSONObject();

        int id = getParamInt("id");
       // System.out.println("id=" + id);
        //根据用户名和密码去进行数据库访问
        Pagination<Studentofclass> pg = new Pagination<>();
        pg.appendWhere("where studentid=?", id);
        getDBConn().select(Studentofclass.class, pg);
        Studentofclass studentofclass = pg.getFirstResult();
        if (studentofclass != null) {

            json.put("succ", true);
            json.put("tclass", studentofclass);
            toJson(json.toString());
        }
    }

    /**
     * 根据id删除数据
     * @throws Exception
     */
    public void del() throws Exception {
        if (!panduan()){
            return;
        }
        int id = getParamInt("id");
        System.out.println("del"+id);
        if (id>1){
            Pagination<Studentofclass> pg = new Pagination<>();
            pg.appendWhere("where studentid=?", id);
            getDBConn().select(Studentofclass.class, pg);
            Studentofclass studentofclass = pg.getFirstResult();
            getDBConn().delete(studentofclass);
        }
    }
    /**
     *
     *
     * @throws Exception
     */
    public void update() throws Exception {
        if (!panduan()){
            return;
        }
        JSONObject json = new JSONObject();

        int id=getParamInt("id");
        int classid=getParamInt("classid");
        Pagination<Studentofclass> pg = new Pagination<>();
        pg.appendWhere("where studentid=?", id);
        getDBConn().select(Studentofclass.class, pg);
        Studentofclass studentofclass = pg.getFirstResult();
        studentofclass.setClassid(classid);
        getDBConn().save(studentofclass);
        json.put("succ", true);
        json.put("stmt", "更改成功");
        toJson(json.toString());
    }


}
